//
//  WCBAddAdressTableViewCell.m
//  WeChatBuniess
//
//  Created by TFAppleWork-Summer on 2017/12/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBAddAdressTableViewCell.h"
#import "WCBPlaceholderTextView.h"
#import "WCBAdressModel.h"
#import "WCBAdressViewModel.h"
#import "UIView+Layout.h"
#import "UIScreen+Extend.h"
@interface WCBAddAdressTableViewCell()<UITextViewDelegate>

@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UILabel *placeHodelerLabel;
@property (nonatomic,strong) UILabel *rightLabel;
@property (nonatomic,strong) UIButton *selectBtn;

@property (strong, nonatomic) UITapGestureRecognizer *areaTapGuesture;
@end


@implementation WCBAddAdressTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.textView];
        [self.contentView addSubview:self.rightLabel];
        [self.contentView addSubview:self.selectBtn];
        self.selectionStyle = UITableViewCellSelectionStyleNone;        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x, y, w, h;
    CGFloat width = CGRectGetWidth(self.contentView.frame);
    CGFloat height = CGRectGetHeight(self.contentView.frame);
    
    {
        // selectButton
        w = 40.f;
        h = w;
        x = width - w - 30.f;
        y = (height - h) / 2.f;
        self.selectBtn.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // rightLabel
        w = 100.f;
        x = width - w - 30.f;
        y = 0;
        h = height;
        self.rightLabel.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // textView
        x = 20.f;
        y = 5.f;
        w = CGRectGetMinX(self.rightLabel.frame) - x - 10.f;
        h = height - 2 * y;
        self.textView.frame = CGRectMake(x, y, w, h);
        
        // placeholder
        self.placeHodelerLabel.frame = (CGRect){CGPointZero , self.textView.frame.size};
    }
}

#pragma mark - Private
- (BOOL)empty:(NSString *)str
{
    return str == nil || str.length == 0;
}

#pragma mark - Public
- (void)setUserName:(NSString *)userName
{
    if ([self empty:userName] == NO) {
        // 编辑模式
        self.textView.text = userName;
        self.placeHodelerLabel.hidden = YES;
        self.placeHodelerLabel.text = @"";
    }
    else {
        // 新增模式
        self.textView.text = nil;
        self.placeHodelerLabel.hidden = NO;
        self.placeHodelerLabel.text = @"请输入收货人";
    }
    
    [self.textView removeGestureRecognizer:self.areaTapGuesture];
    self.textView.editable = YES;
    self.selectBtn.hidden = YES;
    self.rightLabel.hidden = YES;
}


- (void)setPhoneNumber:(NSString *)phone
{
    if ([self empty:phone] == NO) {
        // 编辑模式
        self.textView.text = phone;
        self.placeHodelerLabel.hidden = YES;
        self.placeHodelerLabel.text = @"";
    }
    else {
        // 新增模式
        self.textView.text = nil;
        self.placeHodelerLabel.hidden = NO;
        self.placeHodelerLabel.text = @"请输入联系电话";
    }
    
    [self.textView removeGestureRecognizer:self.areaTapGuesture];
    
    self.textView.editable = YES;
    self.selectBtn.hidden = YES;
    self.rightLabel.hidden = YES;
}

- (void)setArea:(NSString *)area
{
    if ([self empty:area] == NO) {
        // 编辑模式
        self.textView.text = area;
        self.placeHodelerLabel.hidden = YES;
        self.placeHodelerLabel.text = @"";
    }
    else {
        // 新增模式
        self.textView.text = nil;
        self.placeHodelerLabel.hidden = NO;
        self.placeHodelerLabel.text = @"请选择所在区域";
    }
    
    [self.textView addGestureRecognizer:self.areaTapGuesture];
    
    self.textView.editable = NO;
    self.rightLabel.hidden = NO;
    self.selectBtn.hidden = YES;
}

- (void)setAddress:(NSString *)address
{
    if ([self empty:address] == NO) {
        // 编辑模式
        self.textView.text = address;
        self.placeHodelerLabel.hidden = YES;
        self.placeHodelerLabel.text = @"";
    }
    else {
        // 新增模式
        self.textView.text = nil;
        self.placeHodelerLabel.hidden = NO;
        self.placeHodelerLabel.text = @"请输入详细地址";
    }
    
    [self.textView removeGestureRecognizer:self.areaTapGuesture];
    
    self.textView.editable = YES;
    self.rightLabel.hidden = YES;
    self.selectBtn.hidden = YES;
}


- (void)setDefaultAddress:(BOOL)isDefault
{
    self.textView.text = @"设置为默认地址";
    self.selectBtn.hidden = NO;
    self.selectBtn.selected = isDefault;
    
    [self.textView removeGestureRecognizer:self.areaTapGuesture];
    
    self.textView.editable = NO;
    self.placeHodelerLabel.hidden = YES;
    self.rightLabel.hidden = YES;
}


#pragma mark - action
- (void)selectBtnClick:(UIButton *)button{
    self.selectBtn.selected = !self.selectBtn.selected;
    
    if (self.setDefaultDidClick) {
        self.setDefaultDidClick(self.selectBtn.selected);
    }
}

- (void)tap:(UITapGestureRecognizer *)tap{
    if (self.tapBlock) {
        self.tapBlock();
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if (textView == self.textView) {
        self.placeHodelerLabel.hidden = YES;
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        self.placeHodelerLabel.hidden = NO;
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if (self.textChangedBlock) {
        self.textChangedBlock(textView.text);
    }
}

#pragma mark - lazy - load
- (UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.frame = CGRectMake(30, 10,self.contentView.width - 2 * 30, 40);
        _textView.font = [UIFont systemFontOfSize:23];
        [_textView addSubview:self.placeHodelerLabel];
        
        _textView.delegate = self;
        _textView.scrollEnabled = NO;
        
        self.areaTapGuesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [_textView addGestureRecognizer:self.areaTapGuesture];
    }
    return _textView;
}


-(UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc]init];
        _rightLabel.textColor = [UIColor lightGrayColor];
        _rightLabel.text = @"请选择 >";
        _rightLabel.textAlignment = NSTextAlignmentRight;
        _rightLabel.font = [UIFont systemFontOfSize:23];
    }
    return _rightLabel;
}

- (UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"adressSelect"] forState:UIControlStateSelected];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"adressNotSelect"] forState:UIControlStateNormal];
        [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (UILabel *)placeHodelerLabel{
    if (!_placeHodelerLabel) {
        _placeHodelerLabel = [[UILabel alloc] init];
        _placeHodelerLabel.font = [UIFont systemFontOfSize:23];
        _placeHodelerLabel.textColor = [UIColor lightGrayColor];
    }
    return _placeHodelerLabel;
}

@end
